package com.woniuxy.lrz.carlproject.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.Log;
import com.woniuxy.lrz.carlproject.common.Result;
import com.woniuxy.lrz.carlproject.controller.form.shopform.updatePwdForm;
import com.woniuxy.lrz.carlproject.service.dto.ShopDto.ShopDto;
import com.woniuxy.lrz.carlproject.controller.form.shopform.ShopLoginForm;
import com.woniuxy.lrz.carlproject.controller.form.shopform.ShopUpdateForm;
import com.woniuxy.lrz.carlproject.model.Shop;
import com.woniuxy.lrz.carlproject.service.ShopService;
import com.woniuxy.lrz.carlproject.service.dto.ShopDto.ShopListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 门店表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@RequestMapping("/shop")
@Slf4j
@Api("门店操作接口")
public class ShopController {


    @Resource
    private ShopService shopService;

    @GetMapping("/login")
    @Log("门店登录")
    @ApiOperation("门店登录")
    @ApiImplicitParam("门店账号密码表单")
    public Result login(@Valid  ShopLoginForm shopLoginForm){
        log.info("登录信息:{}",shopLoginForm);
        if (shopLoginForm==null){
            throw  new AppException("参数错误");
        }
        String s =shopService.login(shopLoginForm);
        return  Result.success(s);
    }

    @GetMapping("/getShop")
    @Log("查询门店详情")
    @ApiOperation("查询门店详情")
    public Result getShop(){
        int id = Integer.parseInt((String) StpUtil.getLoginId());
        log.info("门店id:{}",id);
        if (id<1){
            throw new AppException("参数错误");
        }
        Shop s = shopService.getById(id);
        ShopDto shopDTO = BeanUtil.copyProperties(s, ShopDto.class);
        return Result.success(shopDTO);
    }


    @PutMapping("/updateInfo")
    @Log("门店信息修改")
    @ApiOperation("门店信息修改")
    @ApiImplicitParam("门店信息对象")
    public Result updateInfo(@RequestBody @Valid  ShopUpdateForm shopUpdateForm){
       log.info("门店信息修改表单:{}",shopUpdateForm);
       int shopId = Integer.parseInt((String) StpUtil.getLoginId());

        shopService.updateByShopId(shopId,shopUpdateForm);
        return Result.success();
    }

    @PutMapping("/updatePwd")
    @Log("修改密码")
    @ApiOperation("修改密码")
    @ApiImplicitParam("密码表单")
    public  Result updatePwd(@RequestBody @Valid updatePwdForm updatePwdForm){
        log.info("密码表单：{}",updatePwdForm);
        int shopId = Integer.parseInt((String) StpUtil.getLoginId());

        UpdateWrapper<Shop> wrapper = new UpdateWrapper<>();
        wrapper.eq("shop_id",shopId)
               .eq("shop_pwd",DigestUtils.sha256Hex(updatePwdForm.getOldShopPwd()).toString())
               .set("shop_pwd",DigestUtils.sha256Hex(updatePwdForm.getShopPwd()).toString());
        boolean update = shopService.update(wrapper);
        if (!update){
            throw  new AppException("修改密码失败");
        }
        return Result.success();
    }

    //根据服务详情id查询门店列表
    @GetMapping("/getShopById")
    @Log("根据服务详情id查询门店列表")
    @ApiOperation("根据服务详情id查询门店列表")
    public Result getShopById(Integer serviceId){
        List<ShopListDto> shops = shopService.getShopById(serviceId);
        return Result.success(shops);
    }


}
